{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyparsing import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "\n/home/siva\n['/home/siva']\n\ntest3/\n['test3/']\n\ntest/*\n     ^\nFAIL: Expected end of text, found '*'  (at char 5), (line:1, col:6)\n\n*\n^\nFAIL: Expected {W:(ABCD...) | \"..\" | \".\" | \"\\*\"}, found '*'  (at char 0), (line:1, col:1)\n\n..\n['..']\n\n.\n['.']\n\ngocalFile\n['gocalFile']\n\n/home/siva.txt\n['/home/siva.txt']\n\n../test2\n['../test2']\n\n./test4\n['./test4']\n\n/tmp/test4/test5/test6/test7\n['/tmp/test4/test5/test6/test7']\n\n/tmp/test a'/test b/test c\n            ^\nFAIL: Expected end of text, found '/'  (at char 12), (line:1, col:13)\n"
    }
   ],
   "source": [
    "\n",
    "BashQuote=oneOf(\"\\' \\\"\")[...]\n",
    "BashPath=BashQuote + Combine(Word(alphanums + \" _.-/\") | \"..\" | \".\" | \"\\*\") + BashQuote # TODO:*\n",
    "#BashPath=Combine(OneOrMore(Word(\"/\", alphanums)))\n",
    "\n",
    "\n",
    "for inputPath in [\n",
    "    \"/home/siva\",\n",
    "    \"test3/\",\n",
    "    \"test/*\",\n",
    "    \"*\",\n",
    "    \"..\",\n",
    "    \".\",\n",
    "    \"gocalFile\",\n",
    "    \"/home/siva.txt\",\n",
    "    \"../test2\",\n",
    "    \"./test4\",\n",
    "    \"/tmp/test4/test5/test6/test7\",\n",
    "    \"/tmp/test a'/test b/test c\"\n",
    "]:\n",
    "    BashPath.runTests(inputPath)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "BashIdentifier=Word(alphanums + \"_-/$\")\t\n",
    "BashCommand=Word(alphanums + \"_\").setResultsName(\"command\") + BashPath[...]\n",
    "BashFlag=Combine(OneOrMore(\"-\") + OneOrMore(alphanums))\n",
    "#BashArgument=Combine((BashIdentifier[...]+BashFlag[...]+BashPath[...])[1,...])\n",
    "BashRedirect=oneOf(\"> >> \") + BashPath[1,...]\n",
    "BashSimpleCommand=BashIdentifier.setResultsName(\"command\") + BashFlag[...] + BashPath[...] + BashIdentifier[...] +  BashRedirect[...]  # + BashArgument[1,...]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def parseSimpleCommand(stmt):\n",
    "    try:\n",
    "        tokens = BashSimpleCommand.parseString(stmt)\n",
    "        print(tokens.command)\n",
    "        return tokens.command\n",
    "    except ParseException:\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "ls\nls\nls\ncat\ncat\nmkdir\nset\n"
    }
   ],
   "source": [
    "for inputstr in [\n",
    "    \"ls > test\",\n",
    "    \"ls >> test\",\n",
    "    \"ls -l >> test\",\n",
    "    \"cat hi | grep -i test\",\n",
    "    \"cat -n -f test\",\n",
    "    \"mkdir /super/test\",\n",
    "    \"set -o trap\"\n",
    "]:\n",
    "    parseSimpleCommand(inputstr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}